import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.options import Options as EdgeOptions
import subprocess  
from datetime import datetime

# 命令行参数传递:session.config.getoption('--platformName'); request.config.getoption('--platformName')
def pytest_addoption(parser):
    print("-----获取输入的参数至------")
    # parser.addoption("--alluredir", action="store", default="chrome", help="alluredir to run tests on.")
    parser.addoption("--allure_reports", action="store", default="chrome", help="allure_reports to run tests on.")
    parser.addoption("--allure_reports_url", action="store", default="chrome", help="allure_reports_url to run tests on.")
    parser.addoption("--browser", action="store", default="chrome", help="Browser to run tests on.")
    parser.addoption("--headless", action="store_true",default=False, help="Run tests in headless mode.")
    parser.addoption("--webdriver_url_selenium", action="store", help="URL of the webdriver_url_selenium WebDriver server.")
    parser.addoption("--allure_reports_url_port", action="store", help="URL of the allure_reports_url_port= WebDriver server.")

# 测试会话开始前被调用
def pytest_sessionstart(session):
    print("-----web测试会话开始------")

# 测试会话(成功/失败/跳过)结束后被调用
def pytest_sessionfinish(session,exitstatus):
    print("-----web测试会话结束------")
    # 生成测试报告,打开网页展示报告
    alluredir=session.config.getoption('--alluredir')
    allure_reports=session.config.getoption('--allure_reports')
    browser=session.config.getoption('--browser')
    allure_reports_url=session.config.getoption('--allure_reports_url')
    allure_reports_url_port=session.config.getoption('--allure_reports_url_port')

    # 获取当前时间作为目录名
    # timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    # # 生成测试报告
    # generate_command = f'allure generate "{alluredir}" -o "{allure_reports}/"'+browser+'/'+timestamp
    # try:
    #     subprocess.run(generate_command, shell=True, check=True)
    # except subprocess.CalledProcessError as e:
    #     print(f"Failed to generate Allure report: {e}")
    
    # 打开测试报告
    # if allure_reports_url:
    #     open_command = f'allure open -h "{allure_reports_url}" -p "{allure_reports_url_port}" "{allure_reports}"'
    #     try:
    #         subprocess.run(open_command, shell=True, check=True)
    #     except subprocess.CalledProcessError as e:
    #         print(f"Failed to open Allure report: {e}")

@pytest.fixture(scope="function")
def driver(request):
    browser = request.config.getoption("--browser")
    headless = request.config.getoption("--headless")
    webdriver_url_selenium = request.config.getoption("--webdriver_url_selenium")
    options = None

    if browser == "chrome":
        options = ChromeOptions()
        if  headless:
            options.add_argument("--headless")
    elif browser == "edge":
        options = EdgeOptions()
        if headless:
            options.headless = True            
    elif browser == "firefox":
        options = FirefoxOptions()
        if headless:
            options.headless = True
    else:
        raise ValueError(f"Unsupported browser: {browser}")

    if webdriver_url_selenium:   # 远程服务
        driver = webdriver.Remote(command_executor=webdriver_url_selenium, options=options)
    else:           # 本地服务
        if browser == "chrome":
            driver = webdriver.Chrome(options=options)
        elif browser == "firefox":
            driver = webdriver.Firefox(options=options)
        elif browser == "edge":
            driver = webdriver.Edge(options=options)
    yield driver
    driver.quit()



# driver = webdriver.Remote(url, ChromeOptions())
# driver = webdriver.Chrome(ChromeOptions())

